import { LayerType, LayerItem } from "./ui";
import { IUIView } from "./IUIView";


export default class LayerManager {

    //根节点
    protected root: any;

    //层级管理器的类型
    protected layerType: LayerType = LayerType.SINGLE;

    //管理对象的列表
    protected list: IUIView[]

    //跟管理器的内容是否可以被弹出
    protected popFlag: boolean = false;

    //管理器的层级
    protected zIndex: number = 1;
    protected node: any;

    init(param: LayerItem, node: any) {
        this.list = []
        this.root = node;
        this.layerType = param.layerType;
        this.zIndex = param.zIndex;
        this.popFlag = param.canPop;
    }

    setZOrder(order: number) {
        this.zIndex = order;
    }

    getZOrder(): number {
        return this.zIndex;
    }

    canPop() {
        return this.popFlag;
    }

    isValid() {
        return this.root != null && this.root != undefined;
    }

    count() {
        return this.list.length;
    }

    /**
     * 设置整个管理器内的ui是否可见
     * @param flag 
     */
    setVisible(flag: boolean) {
        for (let index = this.list.length - 1; index >= 0; index--) {
            const element = this.list[index];
            element.setVisible(flag)
        }
    }

    has(layer: IUIView) {
        for (let index = this.list.length - 1; index >= 0; index--) {
            const element = this.list[index];
            if (layer === element) {
                return true;
            }
        }
        return false;
    }

    /**
     * 根据名称判断是否有某个UI。
     */
    // hasView(name: string) {
    //     for (let index = this.list.length - 1; index >= 0; index--) {
    //         const element = this.list[index];
    //         if (element && name == element.getName()) {
    //             return true;
    //         }
    //     }
    //     return false;
    // }

    // hasViewByPath(path: string) {
    //     // console.log(' path ', path)
    //     for (let index = this.list.length - 1; index >= 0; index--) {
    //         const element = this.list[index];
    //         // console.log('hasViewByPath  path ', element.getPath())
    //         if (element && path == element.getPath()) {
    //             return true;
    //         }
    //     }
    //     return false;
    // }

    /**
     * 删除指定名称的UI，有可能有多个指定名称的UI。
     * 如果删除指定的UI，可以使用removeView
     */
    // removeByName(name: string) {
    //     for (let index = this.list.length - 1; index >= 0; index--) {
    //         const element = this.list[index];
    //         if (name == element.getName()) {
    //             this.list.splice(index, 1)
    //             element.exit()
    //             break;
    //         }
    //     }
    // }
    // /**
    //   * 删除指定名称的UI，有可能有多个指定名称的UI。
    //   * 如果删除指定的UI，可以使用removeView
    //   */
    removeByPath(path: string) {
        for (let index = this.list.length - 1; index >= 0; index--) {
            const element = this.list[index];
            if (path == element.getPath()) {
                this.list.splice(index, 1)
                element.exit()
                return true
            }
        }
        return false
    }
    //添加layer
    pushView(view: IUIView): void {
        switch (this.layerType) {
            case LayerType.STACK:
                this.list.push(view);
                this.show(view)
                break;
            case LayerType.QUEUE:
                this.list.push(view);

                if (this.list.length == 1) {
                    this.show(view);
                }
                break;
            case LayerType.SINGLE:
                this.popView(null)
                this.list.push(view);
                this.show(view)

                break;
        }
    }

    // 移除layer
    popView(view: IUIView): IUIView {
        switch (this.layerType) {
            case LayerType.STACK:
                if (!view) {
                    if (this.list.length > 0) {
                        let layerInfo = this.list.pop();
                        layerInfo.exit();
                        return layerInfo;
                    } else {
                        return null;
                    }
                } else {
                    this.removeView(view);
                    return view;
                }
            case LayerType.QUEUE:

                if (!view) {
                    console.log("this.list.length 111 ", this.list.length)
                    if (this.list.length > 0) {
                        let layerInfo = this.list.shift();
                        layerInfo.exit();
                        if (this.list.length > 0) {
                            this.show(this.list[0]);
                        }
                        return layerInfo;
                    } else {
                        return null;
                    }
                } else {
                    console.log("this.list.length 222 ", this.list.length)
                    let flag = this.removeView(view);
                    if (this.list.length > 0) {
                        this.show(this.list[0]);
                    }
                    return view
                }
            case LayerType.SINGLE:
                if (this.list.length > 0) {
                    let layerInfo = this.list.shift();
                    layerInfo.exit();
                    return layerInfo;
                } else {
                    return null;
                }
        }
    }

    show(view: IUIView) {
        this.root.addChild(view.getNode())
        view.setOrder(-1);
    }
    //删除指定位置的layer
    removeView(layer: IUIView): boolean {
        // logInfo(' LayerManger removeView ')
        let index = this.list.indexOf(layer)
        if (index >= 0) {
            this.list.splice(index, 1);
            layer.exit();
            return true;
        }
        return false;
    }

    addView(view: IUIView) {
        this.list.push(view)
    }

    clear() {
        // logInfo(' LayerManger clear ')
        for (let index = this.list.length - 1; index >= 0; index--) {
            const element: IUIView = this.list[index];
            element.exit();
        }
        this.list.length = 0;
    }
}

